﻿using System;
using System.Collections.Generic;
using System.Linq;
using log4net;
using RedundantValuesPlugin.Properties;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using ResxCrunch.Plugins;
using ResxCrunch.Business.Managers;
using ResxCrunch.Utils;

namespace ResxCrunch
{
    [ResxCrunchPlugin]
    public class Plugin : IResxCrunchPlugin
    {
        #region Constants
        /// <summary>
        /// Logger declaration
        /// </summary>
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion Constants

        #region Properties
        public Form ParentForm { get; set; }

        public string ApplicationName { get; set; }
        #endregion Properties

        BackgroundWorker bgWorker = new BackgroundWorker();

        public void InitializePlugin()
        {
            log.DebugFormat("Initialized RedundantPlugin plugin");
        }

        public System.Drawing.Image MenuImage
        {
            get { return Resources.PluginIcon; }
        }

        public string Text
        {
            get { return "Remove redundant items"; }
        }


        /// <summary>
        /// Processes the passed data.
        /// </summary>
        /// <param name="data">The data that needs processing.</param>
        /// <returns>Errormessage or empty string on success</returns>
        public string ProcessData(ref DataTable data, out int counter)
        {
            // How many rows were removed
            counter = 0;

            try
            {
                if (data == null)
                {
                    log.Error("The provided data parameter is null");
                    return "The provided data parameter is null"; ;
                }

                if (data.Rows.Count == 0)
                {
                    return "Data contained no rows";
                }

                if (MessageBox.Show(this.ParentForm, "Are you sure you wish remove the rows that contain the same value for every language?", this.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    List<string> cultures = DataMngr.Instance.ExtractCulturesFromResxCrunchDataSource(data);

                    for (int i = data.Rows.Count - 1; i >= 0; i--)
                    {
                        bool sameValues = true;

                        // Only remove the ones that contain values. There is another operation that does empty rows
                        if (data.Rows[i][Enums.DataColumn.DefaultValue].ToString().Length > 0)
                        {
                            foreach (string c in cultures)
                            {
                                // If all equal default value column, then the row contains the same values, so it can be removed
                                if (!data.Rows[i][c].ToString().Equals(data.Rows[i][Enums.DataColumn.DefaultValue].ToString()))
                                {
                                    sameValues = false;
                                    break;
                                }
                            }

                            // If all the values are the same the row needs to be removed
                            if (sameValues)
                            {
                                data.Rows[i].Delete();
                                counter++;
                            }
                        }
                    }

                    data.AcceptChanges();
                }
                else
                {
                    return "Cancelled";
                }

                return string.Empty;
            }
            catch (Exception ex)
            {
                log.Error("Error executing plugin.", ex);
                return "Error executing plugin.";
            }
        }
        #region IResxCrunchPlugin Members

        #endregion
    }
}
